;; Based on: https://gitlab.com/ambrevar/dotfiles/-/blob/master/.emacs.d/lisp/main.el

;; Personal Information
(setq user-full-name "Rafael Mironiuc Marte")
(setq user-mail-address "rxf4e1@tuta.io")

;; Set locale to UTF8
(set-language-environment 'utf-8)
(set-terminal-coding-system 'utf-8)
(setq locale-coding-system 'utf-8)
(set-default-coding-systems 'utf-8)
(set-selection-coding-system 'utf-8)
(prefer-coding-system 'utf-8)

;;; In some cases, Emacs can still decide by itself to use graphical boxes.
;;; Force on using the minibuffer instead.
(setq use-dialog-box nil)

(setq
 inhibit-startup-screen t
 recentf-max-saved-items 100
 disabled-command-function nil          ; Enable all disabled commands.
 scroll-error-top-bottom t              ; Alternative scrolling
 kill-whole-line t                      ; Kill whole line including \n.
 scroll-step 1                          ; Line by line scrolling
 calendar-week-start-day 1
 calendar-date-style 'iso
 comint-prompt-read-only t
 comint-input-ignoredups t
 woman-fill-column fill-column
 ;; abbrev-file-name (expand-file-name "abbrev_defs" (getenv "PERSONAL"))
 frame-title-format (concat "%b" (unless (daemonp) " [serverless]"))
 delete-by-moving-to-trash t
 uniquify-buffer-name-style 'forward
 vc-follow-symlinks t       ; Disable prompt (but leave warning) on git symlink.
 sort-fold-case t

;;; Clipboard and primary selection.
 select-enable-primary t
 save-interprogram-paste-before-kill t

;;; Save all visited URLs.
 url-history-track t
 url-history-file (expand-file-name "url/history" user-emacs-directory)

 ;; History duplicates are useless (?) and annoying with recent Helm:
 ;; https://github.com/syl20bnr/spacemacs/issues/13564
 history-delete-duplicates t

 ;; Timeout before echoing the prefix of an unfinished keystroke.
 echo-keystrokes 0.5

 ;; Disable autosave features.
 auto-save-default nil
 auto-save-list-file-prefix nil

 ;; Place backup files in specific directory.
 backup-directory-alist
 `(("." . ,(expand-file-name ".backups" user-emacs-directory)))

 ;; Enforce horizontal splitting. 140 means that the window is large enough to
 ;; hold 2 other windows of 70 columns.
 split-height-threshold nil
 split-width-threshold 140

 ;; TODO: Ediff does not seem to auto-refine.  Bug?  Compare daemon and no-daemon.
 ediff-window-setup-function 'ediff-setup-windows-plain
 ediff-split-window-function 'split-window-horizontally)

;; Fringe-Mode and Style
(fringe-mode '(1 . 0))

;;; Remember last cursor position.
(save-place-mode)
;;; When the daemon is killed abruptly, places are not saved. Adding this hook
;;; allows to save places at a strategic moment.
(add-hook 'before-save-hook 'save-place-kill-emacs-hook)

(savehist-mode) ; Save M-: history.

(setq-default major-mode 'text-mode) ; Default mode.

;;; Disable suspend key since it is useless on Emacs server.
(global-unset-key (kbd "C-z"))
(global-unset-key (kbd "C-x C-z"))

;; Ask "y" or "n" instead of "yes" or "no". Yes, laziness is great.
(defalias 'yes-or-no-p 'y-or-n-p)

(size-indication-mode 1) ; Print buffer size in mode line.

;;; Line numbers
;;; Adding to `find-file-hook' ensures it will work for every file, regardless of
;;; the mode, but it won't work for buffers without files nor on mode change.
(dolist (hook '(prog-mode-hook text-mode-hook))
  (add-hook hook 'rx/turn-on-column-number-mode)
  (add-hook hook 'rx/turn-off-line-number-mode)
  (add-hook hook 'display-line-numbers-mode))
(setq display-line-numbers-type 'visual)
(defun rx/turn-on-absolute-line-number ()
  (setq display-line-numbers-type t))
(setq auto-revert-interval 1)
(add-hook 'auto-revert-tail-mode-hook 'rx/turn-on-absolute-line-number)

;;; Indentation
(setq-default tab-width 2)
;; (defvaralias 'standard-indent 'tab-width)
(setq-default indent-tabs-mode t)

;;; Show matching parenthesis
(show-paren-mode 1)
;;; By default, there’s a small delay before showing a matching parenthesis. Set
;;; it to 0 to deactivate.
(setq show-paren-delay 0
      show-paren-when-point-inside-paren t)

;;; Electric Pairs to auto-complete () [] {} "" etc. It works on regions.
(electric-pair-mode)

;;; Move mouse away.
(mouse-avoidance-mode 'banish)

;;; Replace not-so-useful comment-dwim binding.
(global-set-key (kbd "M-;") 'comment-line)

;;; Replace `kill-buffer' binding by `kill-this-buffer'.
(global-set-key (kbd "C-x k") 'kill-this-buffer)

;; Visual
;;; Font size
(when (fboundp 'tool-bar-mode)
  (when (find-font (font-spec :name "DejaVu Sans Mono-11"))
  ;; (set-face-attribute 'default nil :height 100)
    ;; Emacs raises an error if font is not found.
    (add-to-list 'default-frame-alist '(font . "DejaVu Sans Mono-11"))))

;;; Cursor type: default (box) is visible and practical.
;; (setq-default cursor-type 'hollow)
(setq-default x-stretch-cursor nil)
;;; Blinking cursor is on only when Emacs is not daemonized.
(blink-cursor-mode t)

;;; Auto-fill
(when (getenv "MANWIDTH")
  (setq-default fill-column (string-to-number (getenv "MANWIDTH"))))
(add-hook 'text-mode-hook 'turn-on-auto-fill)

;;; Windmove mode
;;; By default, it allows easy window switching with Shift+arrows. I like to
;;; stick to the home-row, but to avoid shadowing other binding I exceptionaly use
;;; 'super' (normally reserved to the WM).
(when (fboundp 'windmove-default-keybindings)
  (rx/global-set-keys
   "s-<left>" 'windmove-left
   "s-<down>" 'windmove-down
   "s-<up>" 'windmove-up
   "s-<right>" 'windmove-right))
(rx/global-set-keys
 "C-s-k" 'delete-other-windows
 "s-<tab>" 'other-window
 "C-s-c" 'delete-window)

;; Help as Hell
(which-key-mode t)
(global-undo-tree-mode t)

(provide 'main)
